package com.aacar.utils;

import java.io.Serializable;

public class Page implements Serializable {
	private static final long serialVersionUID = 1L;
	private int pageSize;
	private int pageNumber;
	private int total;
	private int startRs;

	
	public Page() {
		pageSize = 20;
		pageNumber = 1;
		total = 0;
		startRs = 0;
	}
	
	public Page(int page) {
		pageSize = 20;
		total = 0;
		startRs = 0;
		this.pageNumber = page;
	}

	public Page(int page, int pageSize) {
		this.pageSize = 20;
		total = 0;
		startRs = 0;
		this.pageNumber = page;
		this.pageSize = pageSize;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageNumber(int pageNumber) {
		if (pageNumber > 1){
			startRs = (pageNumber - 1) * pageSize;
		}
		if (pageNumber > getTotalPage()){
			if(getTotalPage()!=0){
			  startRs = (getTotalPage() - 1) * pageSize;
			}else{
			  startRs = 0;
			}
		}
		this.pageNumber = pageNumber;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setTotal(int total) {
		this.total = total;
		if (pageSize > 0) {
			int pageCount = ((total + pageSize) - 1) / pageSize;
			if (pageNumber <= 0 || 0 == total) {
				setPageNumber(1);
				setStartRs(0);
			} else if (pageNumber > pageCount) {
				setPageNumber(pageCount);
				setStartRs((pageCount - 1) * pageSize);
			} else {
				setStartRs((pageNumber - 1) * pageSize);
			}
		}
	}

	public int getPageNumber() {
		int resPageNumber = this.pageNumber;
		if(pageNumber<1){
			resPageNumber = 1;
		}
		return resPageNumber;
	}

	public int getTotal() {
		return total;
	}
	
	public int getStartRs() {
		if (pageNumber > 1){
			startRs = (pageNumber - 1) * pageSize;
		}
		if (pageNumber > getTotalPage()){
		    if(getTotalPage()!=0){
			  startRs = (getTotalPage()) * pageSize;
			}else{
			  startRs = 0;
			}
		}
		return startRs;
	}

	public void setStartRs(int startRs) {
		this.startRs = startRs;
	}

	public int getTotalPage() {
		int totalPage = 0;
		if(pageSize!=0){
			totalPage = ((total + pageSize) - 1) / pageSize;
		}
		return totalPage;
	}

}
